#include "Camera.h"

CCamera* CCamera::s_pCamera = NULL;

CCamera::CCamera(void):
mCameraHeight(6.0f), mCameraRotationY(1.2*D3DX_PI), mCameraRadius(12.0f)
{
	BuildViewMtx();
	BuildProjMtx();
}

CCamera::~CCamera(void)
{
}


void CCamera::Update(float dt)
{
	// Get snapshot of input devices.
	DirectInput::GetInstance()->poll();

	// Check input.
	if( DirectInput::GetInstance()->keyDown(DIK_W) )	 
		mCameraHeight   += 25.0f * dt;
	if( DirectInput::GetInstance()->keyDown(DIK_S) )	 
		mCameraHeight   -= 25.0f * dt;

	// Divide by 50 to make mouse less sensitive. 
	mCameraRotationY += DirectInput::GetInstance()->mouseDX() / 100.0f;
	mCameraRadius    += DirectInput::GetInstance()->mouseDY() / 10.0f;

	// If we rotate over 360 degrees, just roll back to 0
	if( fabsf(mCameraRotationY) >= 2.0f * D3DX_PI ) 
		mCameraRotationY = 0.0f;

	// Don't let radius get too small.
	if( mCameraRadius < 2.0f )
		mCameraRadius = 2.0f;
}

void CCamera::BuildViewMtx()
{
	float x = mCameraRadius * cosf(mCameraRotationY);
	float z = mCameraRadius * sinf(mCameraRotationY);
	D3DXVECTOR3 pos(x+100, mCameraHeight+100, z+100);
	D3DXVECTOR3 target(0.0f, 2.0f, 0.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXMatrixLookAtLH(&mView, &pos, &target, &up);

	mEyePos = pos;
}

void CCamera::BuildProjMtx()
{
	/*float w = (float)md3dPP.BackBufferWidth;
	float h = (float)md3dPP.BackBufferHeight;*/
	D3DXMatrixPerspectiveFovLH(&mProj, D3DX_PI * 0.25f, 4.0f/3.0f, 1.0f, 5000.0f);
}

D3DXMATRIX CCamera::GetViewMatrix()
{
	return mView;
}

D3DXMATRIX CCamera::GetProjMatrix()
{
	return mProj;
}